﻿using System;
using System.Windows.Input;
using System.ComponentModel;
using Lib.Helper;
using Lib.UserServices;
using MSIACCSL2.Command;
using WcfService1.DTO;

namespace Lib.Approval
{
    public class ApprovalViewModel : ViewModelBase
    {
        private UserServices.UserServicesClient client;
        private UserDTO _user;

        public ApprovalViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                client = new UserServicesClient();
                User = new UserDTO();
                ValidateCommand = new DelegateCommand(validateLogin, CanValidate);
            }
        }

        public UserDTO User
        {
            get { return _user; }
            set
            {
                _user = value;
                FirePropertyChanged("User");
            }
        }

        public ICommand ValidateCommand { get; set; }

        public bool CanValidate(Object param)
        {
            return true;
        }

        public void validateLogin(Object param)
        {
         
            client.ValidateUserCompleted += client_ValidateUserCompleted;
            client.ValidateUserAsync(User);
        }

        private void client_ValidateUserCompleted(object sender, ValidateUserCompletedEventArgs e)
        {
            client.ValidateUserCompleted -= client_ValidateUserCompleted;
            if (e.Result)
            {
              EventAggregator.PublishMessage("ApprovalSucceeded",this);
            }
        }
    }
}
